package comi.example.xiong.bearfruit;


import android.content.Intent;
import android.util.Log;
import android.view.View;


import com.example.softdk.ycshop.R;

import comi.example.xiong.bearfruit.base.BaseActivity;

import comi.example.xiong.bearfruit.base.RequestInterface;
import comi.example.xiong.bearfruit.base.ServerResponse;
import comi.example.xiong.bearfruit.bean.User;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends BaseActivity {

    @Override
    public int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    public void login(View view) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.Login3 requestInterfaceLogin = retrofit.create(RequestInterface.Login3.class);
        Call<ServerResponse<User>> call = requestInterfaceLogin.login("test", "123456");

        call.enqueue(new Callback<ServerResponse<User>>() {

            @Override
            public void onResponse(Call<ServerResponse<User>> call, Response<ServerResponse<User>> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                if (response.body() == null) {
                    toastShort("网络异常，请检查网络配置");
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    User user = response.body().getData();
                    toastShort("登陆成功！");
                    log("");

                    //获取当前应用
                   /* MyApp app = (MyApp) getApplication();
                    //把返回的user保存到myAPP中
                    app.setCurrentUser(user);
                    Log.d(TAG, "onResponse: user^^^^^^^^^^" + user.toString());
                    Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                    //跳转主活动
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();*/
                } else {
                    toastShort("登陆失败");
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<User>> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                toastShort("登陆失败！");
            }
        });
    }
 /* //获取原始数据，对应第一个button
    public void loginGetOriginalData(View view) {
        String accountString = etAccount.getText().toString().trim();
        String passwordString = etPassword.getText().toString().trim();

        //Log.d(TAG, "login: 账号："+accountString+" 密码 "+passwordString);
        //Toast.makeText(LoginActivity.this, "账号是：" + accountString + " 密码是：" + passwordString, Toast.LENGTH_SHORT).show();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.Login2 requestInterfaceLogin = retrofit.create(RequestInterface.Login2.class);
        Call<ResponseBody> call = requestInterfaceLogin.login(accountString, passwordString);

        call.enqueue(new Callback<ResponseBody>() {

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    *//*  Gson gson = new Gson();*//*
                    //获取返回的原始json字符串
                    String jsonString2 = response.body().string();
                    Log.d(TAG, "获取的原始json字符串: " + jsonString2);
                   *//* Type myType = new TypeToken<ServerResponse<User>>() {
                    }.getType();*//*
     *//*byte[] bytes = response.body().bytes();
                    String jsonString = new String(bytes);*//*
     *//* String jsonString3 = "{\"status\":0,\"msg\":\"登录成功\",\"data\":{\"id\":1,\"username\":\"admin\",\"password\":\"\",\"email\":\"admin@happymmall.com\",\"phone\":\"13800138000\",\"question\":\"问题\",\"answer\":\"答案\",\"role\":1,\"createTime\":null,\"updateTime\":null}}";
                    String json2 = response.body().string();*//*


     *//* ServerResponse myResponse = new ServerResponse();
                    User user = new User();
                    user.setAnswer("3ssde");
                    myResponse.setData(user);
                    myResponse.setMsg("msg");
                    myResponse.setStatus(0);
                    String testResponse = gson.toJson(myResponse);
                    Log.d(TAG, "onResponse: ******" + testResponse);*//*

     *//*  ServerResponse<User> response1 = gson.fromJson(jsonString2, myType);
                    if (response1 != null) {
                        Toast.makeText(LoginActivity.this, "登陆成功" + response1.getMsg(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(LoginActivity.this, "解析出了问题", Toast.LENGTH_SHORT).show();
                    }*//*
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                Toast.makeText(LoginActivity.this, "网络请求错误" + t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }*/

    /* //获取产品列表，对应第三个button
     public void getProductList(View view) {

         Retrofit retrofit = new Retrofit.Builder()
                 .baseUrl(RequestInterface.basePath)
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();

         RequestInterface.getProductsList2 getProductsList = retrofit.create(RequestInterface.getProductsList2.class);

         Call<ServerResponse<MyPageInfo<Product>>> call = getProductsList.getProducts("电", 100001, 1, 8, "");

         call.enqueue(new Callback<ServerResponse<MyPageInfo<Product>>>() {

             @Override
             public void onResponse(Call<ServerResponse<MyPageInfo<Product>>> call, Response<ServerResponse<MyPageInfo<Product>>> response) {
 //                Log.d(TAG, "onResponse: " + response.body().getData());
                 //获取返回的数据（就是ServerResponse<T>中的T代表的东西）
                 MyPageInfo<Product> pageInfo = response.body().getData();
                 //在PageInfo中通过list获取到产品的集合（多个product对象）
                 List<Product> productList = pageInfo.getList();
                 //循环遍历，通过log输出产品信息
                 for (Product product : productList) {
                     Log.d(TAG, "onResponse: ####product#######" + product.toString());
                 }
             }

             @Override
             public void onFailure(Call<ServerResponse<MyPageInfo<Product>>> call, Throwable t) {
                 Log.d(TAG, "onFailure: ");
                 Toast.makeText(LoginActivity.this, "网络请求错误" + t.getMessage(), Toast.LENGTH_LONG).show();
             }
         });
     }
 */
}
